package net.scala.dictionary

import java.awt.Color
import javax.swing.UIManager
import javax.swing.plaf.nimbus.NimbusLookAndFeel

import scala.swing._
import scala.swing.event._

/**
 * @author Barudisshu
 */
object Dictionary extends SimpleSwingApplication {

  UIManager.setLookAndFeel(new NimbusLookAndFeel)

  override def top: Frame = new MainFrame {
    // top is a required method
    title = "词汇表"

    // declare Components here
    val button = new Button {
      text = "查找"
      foreground = Color.blue
      borderPainted = true
      enabled = true
      tooltip = "单击查找中文对照"
      font = new java.awt.Font("Microsoft Yahei", java.awt.Font.PLAIN, 12)
    }
    val textField = new TextField {
      text = "请输入要查找的单词"
      font = new java.awt.Font("Microsoft Yahei", java.awt.Font.PLAIN, 12)
    }
    val textArea = new TextArea {
      editable = false
      font = new java.awt.Font("Microsoft Yahei", java.awt.Font.BOLD, 14)
    }
    val flowPanel = new FlowPanel(button)

    val gridPanel = new GridPanel(1, 1) {
      contents += textField
    }

    // choose a top-level Panel and put components in it
    // Components may include other Panels
    contents = new BoxPanel(Orientation.Vertical) {
      contents += gridPanel
      contents += flowPanel
      contents += textArea

      resizable = true
    }
    size = new Dimension(400, 300)
    centerOnScreen()

    menuBar = new MenuBar {
      contents += new Menu("文件") {
        contents += new MenuItem(Action("退出") {
          sys.exit(0)
        })
      }
    }

    // specify which Components produce events of interest
    listenTo(button)
    listenTo(textField)

    // react to events
    reactions += {
      case ButtonClicked(component) if component == button =>
      case EditDone(component) if component == textField =>
        val text = textField.text.trim
        val p = new Prop().tran(text)
        textArea.text = p
    }
  }
}
